package cn.lingyangwl.agile.common.feign.interceptor;

import cn.lingyangwl.agile.common.feign.content.FeignRequestContent;
import cn.lingyangwl.agile.common.feign.content.FeignRequestContentHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;

import java.util.Map;

/**
 * feign 请求拦截器 oauth2 feign token传递
 *
 * @author shenguangyang
 */
@RequiredArgsConstructor
@ConditionalOnClass({FeignAutoConfiguration.class})
public class FeignCommonRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        FeignRequestContent content = FeignRequestContentHolder.get();
        Map<String, String> addHeaders = content.getAddHeaders();
        addHeaders.forEach(template::header);
        FeignRequestContentHolder.remove();
    }
}